package com.jack.lib.base

import android.app.Activity
import android.app.Application
import android.content.Context
import android.content.res.Resources
import android.os.Bundle
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ProcessLifecycleOwner
import com.hjq.language.MultiLanguages
import com.jack.lib.core.ext.fake
import com.jack.lib.core.net.JBaseData
import com.jack.lib.core.net.JNetView
import com.jack.lib.core.ui.JAuto
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshLayout
import me.jessyan.autosize.AutoSizeCompat


/**
 * 作者: jack(黄冲)
 * 邮箱: 907755845@qq.com
 * create on 2018/12/14 9:20
 */
object BaseApplication: JNetView, LifecycleOwner {

    lateinit var app: Application

    init {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout ->
            layout.setDisableContentWhenRefresh(true)
            layout.setEnableOverScrollBounce(true)
            val classicsHeader = ClassicsHeader(context)
            classicsHeader.setTextSizeTime(12f)
            classicsHeader.setTextSizeTitle(12f)
            classicsHeader.setDrawableSize(12f)
            classicsHeader
        }
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator { context: Context, layout: RefreshLayout ->
            //指定为经典Footer，默认是 BallPulseFooter
            layout.setDisableContentWhenLoading(true)
            layout.setEnableOverScrollBounce(true)
            layout.setEnableAutoLoadMore(false)
            layout.setEnableOverScrollDrag(true)
            val classicsFooter = ClassicsFooter(context)
            classicsFooter.setTextSizeTitle(12f)
            classicsFooter.setDrawableSize(12f)
            classicsFooter
        }
    }


    fun init(app: Application) {
        this.app = app
    }

    fun bindLifecycleAndAuto(app: Application) {
        this::app.isInitialized.fake { init(app) }
        app.registerActivityLifecycleCallbacks(object : IActivityLifecycleCallbacks {
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
                if (activity is IActivityCallback) {
                    activity.setAttributesInterceptor(object : IAttributesInterceptor {
                        override fun getResources(resources: Resources): Resources {
                            AutoSizeCompat.autoConvertDensityOfGlobal(resources)
                            return resources
                        }

                        override fun attachBaseContext(newBase: Context): Context {
                            return MultiLanguages.attach(newBase)
                        }
                    })
                }
            }
        })
    }


    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun lifecycle(): Lifecycle = ProcessLifecycleOwner.get().lifecycle

    override fun onFill(bean: JBaseData<*>) {
    }

    override fun onError(cause: Throwable) {

    }

    override fun getLifecycle(): Lifecycle = ProcessLifecycleOwner.get().lifecycle

}